package com.quick.calcu.controller;

import com.quick.calcu.entity.ClassInfo;
import com.quick.calcu.service.ClassInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/classes")
public class ClassInfoController {

    @Autowired
    private ClassInfoService classService;

    // 根据教师ID查询班级
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getClass(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ClassInfo> classInfos = classService.getClassWithTeacher(id);
            if (classInfos != null) {
                result.put("code", "200");
                result.put("message", "查询成功");
                result.put("data", classInfos);
                return ResponseEntity.ok(result);
            } else {
                result.put("code", "200");
                result.put("success", false);
                result.put("message", "您未管理一个班级");
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    // 根据年级查询班级列表
    @GetMapping("/grade/{grade}")
    public ResponseEntity<Map<String, Object>> getClassesByGrade(@PathVariable Integer grade) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ClassInfo> classes = classService.getClassesByGrade(grade);
            result.put("code", "200");
            result.put("message", "查询成功");
            result.put("data", classes);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    // 添加班级
    @PostMapping
    public ResponseEntity<Map<String, Object>> addClass(@RequestBody ClassInfo classInfo) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = classService.save(classInfo);
            result.put("code", "200");
            result.put("data", success);
            result.put("message", success ? "添加成功" : "添加失败");
            return success ? ResponseEntity.ok(result) : ResponseEntity.badRequest().body(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    // 向班级添加学生
    @PostMapping("/{classId}/add-student")
    public ResponseEntity<Map<String, Object>> addStudentToClass(
            @PathVariable Long classId,
            @RequestParam Long studentId) {

        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = classService.addStudentToClass(classId, studentId);
            result.put("success", success);
            result.put("code", "200");
            result.put("data", success);
            result.put("message", success ? "添加成功" : "添加失败");
            return success ? ResponseEntity.ok(result) : ResponseEntity.badRequest().body(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
}